package com.woniuxy.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.woniuxy.entity.CategoryOne;
import com.woniuxy.entity.CategoryTwo;
import com.woniuxy.entity.Product;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.woniuxy.entity.Perms;
import com.woniuxy.entity.Product;
import com.woniuxy.entity.Roles;

import com.woniuxy.entity.Secutity;

import com.woniuxy.entity.User;
import com.woniuxy.mapper.UserMapper;
import com.woniuxy.service.UserService;

import lombok.Data;

@Data
@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private UserMapper userMapper;

	@Override
	public boolean addUser(User user) {
		return userMapper.addUser(user);
	}

	@Override
	public boolean addUserRoles(String account) {

		return userMapper.addUserRoles(account);
	}

	// zzj

	// 根据id查找用户信息
	@Override
	public User find(int uid) {

		return userMapper.find(uid);
	}

	// 根据用户id修改用户密码
	@Override
	public int updatepwdbyuid(int uid, String oldpwd, String newpwd, String account) {
		String password = userMapper.findpwdbyuid(uid);
		// 将用户输入的原密码加密在做对比
		oldpwd = new SimpleHash("MD5", oldpwd, account, 512).toString();
		System.out.println(password);
		if (password.equals(oldpwd)) {
			User user = new User();
			user.setU_id(uid);
			newpwd = new SimpleHash("MD5", newpwd, account, 512).toString();
			user.setPassword(newpwd);
			return userMapper.updatepwdbyuid(user);
		}
		return -1;
	}

	// 更改用户信息
	@Override
	public int upinfo(User user) {

		return userMapper.upinfo(user) > 0 ? 1 : -1;
	}

	// 更改用户头像
	@Override
	public int updateHeadImg(User user) {

		return userMapper.updateHeadImg(user);
	}

	// 通过密保找回密码
	@Override
	public String findPasswordBysecutityquestion(String question, int u_id, String answer) {
		String an = userMapper.findanswer(new Secutity().setQuestion(question).setU_id(u_id));
		if (an != null) {
			if (an.equals(answer)) {
				return userMapper.findpwdbyuid(u_id);
			}
		}
		return null;
	}

	// 查找用户密保问题，用于找回密码
	@Override
	public List<Secutity> findquestion(int u_id) {
		// TODO Auto-generated method stub
		return userMapper.findquestion(u_id);
	}

	// 修改密保问题
	@Override
	public int setsecurity(String question, String answer, int u_id, String newquestion) {
		System.out.println(userMapper.findid(new Secutity().setU_id(u_id).setQuestion(question)));
		return userMapper.updatesecutity(
				new Secutity().setId(userMapper.findid(new Secutity().setU_id(u_id).setQuestion(question)))
						.setAnswer(answer).setQuestion(newquestion)) > 0 ? 1 : -1;
	}

	// 查找用户密码
	@Override
	public String findpwdbyuid(int uid) {
		// TODO Auto-generated method stub
		return userMapper.findpwdbyuid(uid);
	}

	// 查找用户密保问题的数量
	@Override
	public int findSecutityCount(int u_id) {
		// TODO Auto-generated method stub
		return userMapper.findSecutityCount(u_id);
	}

	// 添加密保问题
	@Override
	public int addsecutity(String question, String answer, int u_id) {
		System.out.println(1111);
		return userMapper.addsecutity(new Secutity().setAnswer(answer).setQuestion(question).setU_id(u_id)) > 0 ? 1
				: -1;
	}

	@Override
	public List<Roles> findRolesByUid(int u_id) {
		return userMapper.findRolesByUid(u_id);
	}

	@Override
	public List<Perms> findPermsByRid(int ro_id) {
		return userMapper.findPermsByRid(ro_id);
	}

	@Override
	public User findByAccount(String account) {

		return userMapper.findByAccount(account);
	}

	@Cacheable(value = "findRolesAndPerms") // 加入注解，这个地方的数据要放到redis非关系数据库中
	@Override
	public User findRolesAndPerms(String account) {
		System.out.println("userServiceImpl查询角色权限");
		return userMapper.findRolesAndPerms(account);
	}

	@Transactional
	public int updataStatus(int u_id, Date regtime, int status) {

		return userMapper.updataStatus(u_id, regtime, status);
	}

	@Override
	public User findUserByUid(int u_id) {

		return userMapper.findUserByUid(u_id);
	}

	@Override
	public int batchUpdateUsers(Integer status, int[] ids) {

		return userMapper.batchUpdateUsers(status, ids);
	}

	@Override
	public int batchDeleteUsers(int[] ids) {

		return userMapper.batchDeleteUsers(ids);
	}

	@Override
	public PageInfo<User> allUser(int pageNum, int pageSize) {

		PageHelper.startPage(pageNum, pageSize);
		List<User> list = userMapper.allUser();
		PageInfo<User> page = new PageInfo<User>(list);

		return page;

	}

}
